home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / modules / crypto.js next >
Text File  |  2009-11-24  |  5KB  |  132 lines

  1.  
  2.  
  3. var EXPORTED_SYMBOLS = ["Crypto"];
  4.  
  5.  
  6. /**
  7. * Lazarus Cryptography
  8. * We will use hybrid encryption when saving data to the database.
  9. * this should allow us to encrypt forms with the public key and only require the user to 
  10. * enter their password when they want to retrieve a form.
  11. * Both the public and private (RSA) keys should be kept in the database.
  12. * The public key is kept unencrypted, but the private key will be symetrically encrypted (AES)
  13. * using the users password.
  14. */
  15. var Crypto = {
  16.  
  17.     AESStaticIV: '0gL6pSJ13AKDvo7xNnsZaQ==',
  18.  
  19.     publicKey: null,
  20.     
  21.     privateKey: null,
  22.     
  23.     //flag to indicate if we are currently generating RSA keys
  24.     generatingKeys: false,
  25.     
  26.     crypto: Components.classes["@labs.mozilla.com/Weave/Crypto;1"].createInstance(Components.interfaces.IWeaveCrypto),
  27.     
  28.     /**
  29.     * encrypt a string 
  30.     */
  31.     encrypt: function(str){
  32.     
  33.         //generate a random string with which we're going to encode this data.
  34.         var randKey = this.crypto.generateRandomKey();
  35.         
  36.         //encrypt the data with the random key
  37.         var encData = this.crypto.encrypt(str, randKey, this.publicKey.iv);
  38.         
  39.         //now encrypt the key with our public/private key encryption
  40.         var encKey = this.crypto.wrapSymmetricKey(randKey, this.publicKey.key);
  41.         
  42.         //and return the encrypted key with the data
  43.         return encKey +":"+ encData;
  44.     },    
  45.     
  46.     decrypt: function(encStr){
  47.         //split the string into key/data
  48.         var encKey = encStr.substr(0, encStr.indexOf(":"));
  49.         var encData = encStr.substr(encStr.indexOf(":")+1);
  50.         
  51.         
  52.         //decrypt the random key
  53.         var decKey = this.crypto.unwrapSymmetricKey(encKey, this.privateKey.key,
  54.                                               this.privateKey.password,
  55.                                               this.privateKey.passphraseSalt,
  56.                                               this.privateKey.privkeyWrapIV);
  57.         
  58.         
  59.         
  60.         //then decrypt the data with the unencrypted random key
  61.         return this.crypto.decrypt(encData, decKey, this.privateKey.iv);
  62.     },
  63.     
  64.     
  65.     /**
  66.     * AES encryption/decryption methods appear to take passwords of the exact length (256 bits) 
  67.     */
  68.     fixPassword: function(password){
  69.         //password must be 256 bits long
  70.         var PASSWORD_LEN = 32; //characters
  71.         password = password.substr(0, PASSWORD_LEN);
  72.         var origLen = password.length;
  73.         for (var i=origLen; i<PASSWORD_LEN; i++){
  74.             password += " ";
  75.         }
  76.         return btoa(password);
  77.     },
  78.     
  79.     AESEncrypt: function(str, password){
  80.         return this.crypto.encrypt(str, this.fixPassword(password), this.AESStaticIV);        
  81.     },
  82.     
  83.     AESDecrypt: function(encStr, password){
  84.         try {
  85.             return this.crypto.decrypt(encStr, this.fixPassword(password), this.AESStaticIV);  
  86.         }
  87.         catch(e){
  88.             return false;
  89.         }
  90.     },
  91.     
  92.     
  93.     generateRSAKeyPair: function(){
  94.     
  95.         var privOut = {};
  96.         var pubOut  = {};
  97.         
  98.         var publicKeyIV = this.crypto.generateRandomIV();
  99.         var password = this.crypto.generateRandomBytes(32)
  100.         var salt = this.crypto.generateRandomBytes(32);
  101.         var iv   = this.crypto.generateRandomIV();
  102.         
  103.         try {
  104.             this.generatingKeys = true;
  105.             this.crypto.generateKeypair(password, salt, iv, pubOut, privOut);
  106.             this.generatingKeys = false;
  107.             return {
  108.                 "public": {
  109.                     key: pubOut.value,
  110.                     iv: publicKeyIV,                
  111.                 },
  112.                 "private": {
  113.                     key            : privOut.value,
  114.                     password       : password,
  115.                     passphraseSalt : salt,
  116.                     privkeyWrapIV  : iv,
  117.                     iv             : publicKeyIV
  118.                 }
  119.             }
  120.         }
  121.         catch(e){
  122.             this.generatingKeys = false;
  123.             var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  124.             var scriptError = Components.classes["@mozilla.org/scripterror;1"].createInstance(Components.interfaces.nsIScriptError);
  125.             scriptError.init(e.message, e.fileName, null, e.lineNumber, null, scriptError.errorFlag, "component javascript");
  126.             consoleService.logMessage(scriptError);   
  127.             return false;
  128.         }
  129.     }
  130. };
  131.